﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    //
    public class JLTAB55DOCData : BaseFunction
    {

        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public JLTAB55DOCData()
            : base()
        {

        }
        public JLTAB55DOCData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public JLTAB55DOCData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }

        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                DataRow rowtemp = null;
                rowtemp = INTEXDB.ABKOPF.NewRow();
                rowtemp["ABKMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABKAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABKAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABKOPF(rowtemp);
                Return.Merge(Result);

                rowtemp = INTEXDB.ABPOS.NewRow();
                rowtemp["ABPMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                rowtemp["ABPAUFPOS"] = dwInput["ABPAUFPOS"];
                Result = INTEXDBC.QueryABPOS(rowtemp, 1);
                Return.Merge(Result);
                DataRow row = Return.Tables["ABPOS"].Rows[0];
                rowtemp = INTEXDB.ABPR.NewRow();
                rowtemp["ARMANDANT"] = row["ABPMANDANT"];
                rowtemp["ARAUFKRS"] = row["ABPAUFKRS"];
                rowtemp["ARAUFLFD"] = row["ABPAUFLFD"];
                rowtemp["ARAUFPOS"] = row["ABPAUFPOS"];
                Result = INTEXDBC.QueryABPR(rowtemp, 1);
                Return.Merge(Result);
                rowtemp = INTEXDB.WASTAMM.NewRow();
                rowtemp["WAMANDANT"] = row["ABPMANDANT"];
                rowtemp["WAART"] = row["ABPART"];
                rowtemp["WAVAR"] = Left(row["ABPVAR"].ToString(), 2);
                Result = INTEXDBC.QueryWASTAMM(rowtemp);
                Return.Merge(Result); 

                rowtemp = INTEXDB.WTSTR.NewRow();
                rowtemp["WTIMANDANT"] = row["ABPMANDANT"];
                rowtemp["WTIART"] = row["ABPART"];
                rowtemp["WTIVAR"] = Left(row["ABPVAR"].ToString(), 2);
                Result = INTEXDBC.QueryWTSTR(rowtemp, 1);
                Return.Merge(Result);

                rowtemp = INTEXDB.STUELI.NewRow();
                rowtemp["STUELIMANDANT"] = row["ABPMANDANT"];
                string STUELISAPD = "00000000      " + row["ABPART"].ToString() + Left(row["ABPVAR"].ToString(), 2) + "  ";
                rowtemp["STUELISAPD"] = STUELISAPD;
                Result = INTEXDBC.QuerySTUELI(rowtemp, 1);
                Return.Merge(Result);

                rowtemp = INTEXDB.WASTGA.NewRow();
                rowtemp["WAGMANDANT"] = row["ABPMANDANT"];
                rowtemp["WAGART"] = row["ABPART"];
                rowtemp["WAGVAR"] = Left(row["ABPVAR"].ToString(), 2);
                Result = INTEXDBC.QueryWASTGA(rowtemp, 2);
                Return.Merge(Result); 

                rowtemp = INTEXDB.GAAUF.NewRow();
                rowtemp["GAMANDANT"] = row["ABPMANDANT"];
                rowtemp["GAAUFKRS"] = row["ABPAUFKRS"];
                rowtemp["GAAUFLFD"] = row["ABPAUFLFD"];
                rowtemp["GAAUFPOS"] = row["ABPAUFPOS"];
                Result = INTEXDBC.QueryGAAUF(rowtemp, 2);
                Return.Merge(Result);

                rowtemp = INTEXDB.ABFOLGE.NewRow();
                rowtemp["ABFMANDANT"] = row["ABPMANDANT"];
                rowtemp["ABFAUFKRS"] = row["ABPAUFKRS"];
                rowtemp["ABFAUFLFD"] = row["ABPAUFLFD"];
                rowtemp["ABFAUFPOS"] = row["ABPAUFPOS"];
                Result = INTEXDBC.QueryABFOLGE(rowtemp, 1);
                Return.Merge(Result);
                string processed = "";
                foreach (DataRow rowf in Result.Tables[0].Rows)
                {
                    string ABFLIEFNR = rowf["ABFLIEFNR"].ToString().Trim();

                    if (ABFLIEFNR == "") continue;

                    if (processed.Contains(ABFLIEFNR + ";")) continue;

                    processed += ABFLIEFNR + ";";

                    rowtemp = INTEXDB.LIEFDAT.NewRow();
                    rowtemp["LSMANDANT"] = rowf["ABFMANDANT"];
                    rowtemp["LSLIEFNR"] = rowf["ABFLIEFNR"];
                    Result = INTEXDBC.QueryLIEFDAT(rowtemp, 2);
                    Return.Merge(Result);

                }
                string order = "";
                string remark = "";
                order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + "   ";
                remark += GetCommentsFromTXTIND("AB3", order).Trim();
                order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                remark += GetCommentsFromTXTIND("ABP", order).Trim();
                row["ABPFILLER1"] = remark;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

    }
}